<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="mini" placeholder="请选择年份" value="">
        <el-option v-for="item in yearList" :key="item" :label="`${item}年`" :value="item" />
      </el-select>
      <el-select
        v-model="currentMonth"
        size="mini"
        style="margin-left: 20px;"
        placeholder="请选择月份"
      >
        <el-option v-for="item in 12" :key="item" :label="`${item}月`" :value="item" />
      </el-select>
    </el-row>

    <el-calendar v-model="currentDate">
      <template #dateCell="{date, data}">
        <div class="date-content">
          <span>{{ data.day | formatDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
// 分析需求：
// yearList 初始化的时候进行计算 当前时间
export default {
  name: 'Work',
  filters: {
    formatDay(day) {
      const res = day.split('-')
      return res[2]
    }
  },
  data() {
    return {
      currentMonth: 1,
      yearList: [2022, 2021],
      startDate: new Date(), // 默认的初始时间
      currentYear: null
    }
  },
  computed: {
    currentDate() {
      return new Date(`${this.currentYear}-${this.currentMonth}-1`)
    }
  },
  created() {
    // 获取当前的年份
    // 基于初始值进行计算当前的年份和月份
    this.currentYear = this.startDate.getFullYear()
    this.currentMonth = this.startDate.getMonth() + 1
    // 得到当前年份向前推5年向后推5年的数组列表(用户下拉框的渲染)
    this.yearList = Array.from(Array(11), (item, index) => {
      return this.currentYear + index - 5
    })
  },
  methods: {
    isWeek(date) {
    //  判断周末
    //   console.log(date.getDay())
      const week = date.getDay()
      return week === 6 || week === 0
    }
  }
}
</script>
<style  scoped>
/deep/ .el-calendar-day {
  height:  auto;
}
/deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;

}
/deep/ .el-calendar-table td.is-selected .text{
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
/deep/ .el-calendar__header {
  display: none
}
</style>

